package smart.jrsoft.email;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*; 
import javax.mail.internet.AddressException;

 
public class SendEmail
{
	public boolean EmailSend(String ToEmailStr,String Title,String Content,String FromEmail) throws AddressException
    {
		//ToEmailStr收件人地址
		//Title邮件标题
		//Content邮件内容
		//FromEmail发件人地址
		 boolean fnt = false;
		 Properties   props   =   new   Properties();   
         Session   sendMailSession;   
         Transport   transport;   
         sendMailSession   =   Session.getInstance(props,   null);   
         props.put("mail.smtp.host",   "10.1.3.238");  
         props.put("mail.smtp.auth",   "true");     //允许smtp校验   
   
         try
         {
	         transport   =   sendMailSession.getTransport("smtp");   
	         transport.connect("10.1.3.238","dmu_service","p@ssword123!");    //用户名,密码...........   
	         Message   newMessage   =   new   MimeMessage(sendMailSession);   
	   
	         //设置mail主题   
	         String   mail_subject=Title;   
	         newMessage.setSubject(mail_subject);   
	   
	       //设置发信人地址   
	       String   strFrom="dmu_service@mychery.com";     //     
	       strFrom=new   String(strFrom.getBytes(),"8859_1");   
	       newMessage.setFrom(new   InternetAddress(strFrom));   
	       Address address[] = {new InternetAddress(FromEmail)};//改变发件人地址
	       newMessage.addFrom(address);
	       //设置收件人地址   
	       newMessage.setRecipient(Message.RecipientType.TO,   new   InternetAddress(ToEmailStr));   
	   
	       //设置mail正文   
	       newMessage.setSentDate(new   java.util.Date());   
	       String   mail_text=Content;
	       newMessage.setText(mail_text);   
	   
	       newMessage.saveChanges();     //保存发送信息   
	       transport.sendMessage(newMessage,newMessage.getRecipients(Message.RecipientType.TO));     //发送邮件   
		   fnt=true;
	       transport.close();   
         }
         catch(Exception e)
         {
			fnt=false;
         	System.out.println(e); 
         }
		return fnt;

    }
	public static  void main(String[] args)
	{
		SendEmail es = new SendEmail();
		String ToEmailStr="919082823@qq.com";
		String Title="测试发送邮件组件";
		String Content="做个测试";
		String FromEmail="dmu_service@mychery.com";
		try {
			boolean fnt=es.EmailSend(ToEmailStr, Title, Content, FromEmail);
			System.out.println(fnt);
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}